/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package GUI.Components;

import LogParser.CommandsHistory;
import Tools.CMDThreadGUI;
import Tools.TModel;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;

/**
 *
 * @author Иван
 */
public class PanelCMD extends AComponentBuilder {

    private final TModel model;
    private JPanel panel = new JPanel();
    private JTextField cmdField = new JTextField();
    private JLabel cmdPuth = new JLabel();
    private CommandsHistory history;
    private final AObserverGUI observerGUI;
    private String dir;

    public PanelCMD(TModel model, AObserverGUI observerGUI, CommandsHistory history) {
        this.history = history;
        this.observerGUI = observerGUI;
        this.model = model;
        panel = new JPanel();
        panel.setBackground(Color.GREEN);

    }
    private KeyListener action = new KeyListener() {

        @Override
        public void keyTyped(KeyEvent e) {
        }

        @Override
        public void keyPressed(KeyEvent e) {
            Activate();
            if (e.getKeyCode() == KeyEvent.VK_UP) {
                try {
                    String s = history.back();
                    if (!"".equals(s)) {
                        cmdField.setText(s);
                    }
                } catch (Exception ex) {
                    JOptionPane.showConfirmDialog(observerGUI.getGUI().getRootPane(), ex.getMessage(), "Error", JOptionPane.ERROR, JOptionPane.ERROR_MESSAGE);
                }

            } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                try {
                    String s = history.next();
                    if (!"".equals(s)) {
                        cmdField.setText(s);
                    }

                } catch (Exception ex) {
                    JOptionPane.showConfirmDialog(observerGUI.getGUI(), ex.getMessage(), "Error", JOptionPane.ERROR, JOptionPane.ERROR_MESSAGE);
                }

            } else if (e.getKeyCode() == KeyEvent.VK_ENTER) {

                observerGUI.setCommandThread(new CMDThreadGUI(cmdField.getText(), observerGUI));
            }
        }

        @Override
        public void keyReleased(KeyEvent e) {
        }
    };

    @Override
    public JPanel buildComponent() throws Exception {
        cmdField = new JTextField();
        cmdField.setPreferredSize(new Dimension(300, 20));

        cmdField.addKeyListener(action);
        cmdPuth = new JLabel(model.getWorkDirectory());

        panel.add(cmdPuth);
        panel.add(cmdField);

        return panel;
    }

    @Override
    public void updateComponent(Object object) {
        cmdPuth.setText(model.getWorkDirectory());
        cmdField.setText("");
    }

    public String getUserCommand(String commandName) {
        return observerGUI.buildCommand(commandName);
    }
}
